package com.chenchao.thread;

import org.apache.commons.io.FileUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @encoding : UTF-8
 * @Time : 2020/11/16 18:48
 * @Author : Chen Chao
 * @File : javaSE.com.chenchao.javathread.Test6.java
 * @Software : IntelliJ IDEA
 * @since : 1.8
 * 多线程下载网络图片
 */
public class Test6 {
    private final static String PATH = "myjava01/src/fo/URL.txt";
    private final static String DES = "myjava01/src/img/";
    private static int num = 0;

    /**
     * 多线程，同时下载
     *
     * @param args args
     */
    public static void main(String[] args) {

        allGO();
        allGO();
        allGO();
    }

    /**
     * 单一线程下载
     */
    public static void allGO() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                go();
            }
        }).start();

    }

    /**
     * 一次下载
     */
    public static void go() {
        int i = 0;
        while (num < 1000) {
            synchronized (Test6.class) {
                i = num++;
            }
            downLoad(createList(readURL(), i));
            if (i == 1000) {
                System.out.println("下载完成！");
            }
        }
    }

    /**
     * 根据传入的实例对象下载文件
     *
     * @param heroList 传入的实例对象
     */
    public static void downLoad(HeroList heroList) {
        if (heroList != null) {
            try {
                FileUtils.copyURLToFile(heroList.getUrl(), new File(Test6.DES.concat(heroList.getnum() + ".jpg")));
                System.out.println(heroList.getnum() + "下载成功");
            } catch (IOException e) {
                System.out.println("文件不存在，URL有问题");
            }
        }
    }

    /**
     * 根据传入的URL和数字编号生成实例对象
     *
     * @param str 网站名
     * @param num 编号
     * @return 实例对象
     */
    public static HeroList createList(String str, int num) {
        StringBuilder snum = new StringBuilder();
        snum.append(num / 100);
        snum.append(num % 100 / 10);
        snum.append(num % 10);
        str += "/" + num + "/" + num + ".jpg";
        try {
            URL url = new URL(str);
            return new HeroList(snum.toString(), url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        }


    }

    /**
     * 从文件中读取url字符串
     *
     * @return url字符串
     */
    public static String readURL() {
        try (BufferedReader bufferedReader = new BufferedReader(new FileReader(new File(Test6.PATH)));) {
            String str = bufferedReader.readLine();
            return str;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}

/**
 * 把英雄序号，和英雄图片包装成一个类
 */
class HeroList {
    private String num;
    private URL url;

    public HeroList() {
    }

    public HeroList(String num, URL url) {
        this.num = num;
        this.url = url;
    }

    public String getnum() {
        return num;
    }

    public void setnum(String num) {
        this.num = num;
    }

    public URL getUrl() {
        return url;
    }

    public void setUrl(URL url) {
        this.url = url;
    }
}